package py.edu.ucom.cadira.game.war;

import org.jdom.Element;

import py.edu.ucom.cadira.net.User;

/**
 * 
 * @author largonet
 * 
 */
public class WarUser extends User{
    
    private boolean statusBoard = false;
    private int color;
    private int typeUser;
    public static int PLAYER = 0;
    public static int WATCHER = 1;
    

    private User realUser;
    public WarUser(){
        
    }
    public WarUser(User realUser){
    	this.realUser=realUser;
        this.username = realUser.getUsername();
        this.nombre = realUser.getNombre();
    }
    public WarUser(User realUser, int color){
        this.realUser=realUser;
        this.username = realUser.getUsername();
        this.nombre = realUser.getNombre();
        this.color = color;
    }
    public WarUser(String nombre, String username) {
        this.nombre = nombre;
        this.username = username;
    }
    public int getColor() {
        return color;
    }
    public String getColorString() {
        if (color == WarGame.COLOR_RED) {
            return "RED";
        } else if (color == WarGame.COLOR_BLUE) {
            return "BLUE";
        } else {
            return "UNALLOCATED";
        }
    }
    public void setColor(int color) {
        this.color = color;
    }
    public void setColor(String color){
        if (color.toUpperCase().compareTo("RED")==0){
            setColor(WarGame.COLOR_RED);
        }else{
            if (color.toUpperCase().compareTo("BLUE")==0){
                setColor(WarGame.COLOR_BLUE);
                }
            }
    }
   
    public void setTypeUser(int type) {
        this.typeUser = type;
    }
    public int getTypeUser() {
        return typeUser;
    }
    /**
     * @return Returns the status.
     */
    public boolean isStatusBoard() {
        return statusBoard;
    }
    /**
     * @param status The status to set.
     */
    public void setstatusBoard(boolean status) {
        this.statusBoard = status;
    }
    public String toString(){
        StringBuffer buff=new StringBuffer();
        buff.append(username);
        
        return buff.toString();
    }
    
	public User getRealUser() {
		return realUser;
	}
	public void setRealUser(User realUser) {
		this.realUser = realUser;
	}
    public Element toXml(){
        Element nodeUser=super.toXml();
        if(typeUser==PLAYER){
            Element nodeColor=new Element("color");
            nodeColor.addContent(getColorString());
            nodeUser.addContent(nodeColor);
        }
        
        return nodeUser; 
    }
    /**
     * problema al querer setear el color... por que no tengo forma de saber si es
     * watcher o player
     */
    public WarUser fromXml(Element userNode){
        User user = super.fromXml(userNode);
        Element nodeColor = userNode.getChild("color");
        this.setUsername(user.getUsername());
        if (nodeColor.getValue() != null) 
            this.setColor(nodeColor.getValue());
        return this;
        
    }

}
